﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using HCSExtensions;
using System.Net;
using DAL;
using System.Xml.Linq;
using Dynamics.Enums;
using Dynamics.Models;
using System.Threading;
using System.Globalization;

public class DataHelper
{
    public static TransactionEvent GetData(string url)
    {
        TransactionEvent transaction = new TransactionEvent();
        string result = string.Empty;

        try
        {
            WebClient webClient = new WebClient();
            webClient.Encoding = Encoding.UTF8;
            result = webClient.DownloadString(url);
            transaction.ReturnObject = result;
        }
        catch (Exception ex)
        {
            transaction.Exception = ex;
            return transaction;
        }

        return transaction;
    }

    public static TransactionEvent ParseProductsFromXML(string data)
    {
        CultureInfo cultureInfo = new CultureInfo("tr-TR");
        Thread.CurrentThread.CurrentCulture = cultureInfo;
        Thread.CurrentThread.CurrentUICulture = cultureInfo;

        TransactionEvent transaction = new TransactionEvent();
        List<Product> products = new List<Product>();
        List<ProductErrorLogItem> errorLogs = new List<ProductErrorLogItem>();
        XDocument productXML = null;
        try
        {
            productXML = XDocument.Parse(data);
        }
        catch (Exception ex)
        {
            transaction.ErrorCode = (byte)LogType.DataParseError;
            transaction.Exception = ex;
            return transaction;
        }

        if (productXML.Root.Elements().Count() == 0 || productXML.Root.IsEmpty)
        {
            return transaction;
        }

        List<XElement> productElements = productXML.Root.Elements().Where(t => t.Name.LocalName == "item").ToList();

        if (productElements.Count == 0)
        {
            return transaction;
        }

        Product product = null;
        foreach (XElement productElement in productElements)
        {
            try
            {
                product = new Product();
                product.Heading = GetElementValue(productElement, "title");
                product.SiteProductID = GetElementValue(productElement, "id");
                product.Price = Convert.ToDecimal(GetElementValue(productElement, "deal_price"));
                product.OldPrice = Convert.ToDecimal(GetElementValue(productElement, "real_price"));
                product.Currency = GetElementValue(productElement, "currency");
                if (string.IsNullOrEmpty(product.Currency))
                {
                    product.Currency = "TL";
                }
                product.Description = GetElementValue(productElement, "description");
                product.Detail = GetElementValue(productElement, "detail");
                product.HowToUse = GetElementValue(productElement, "howToUse");
                product.ImageUrl = GetElementValue(productElement, "image");
                product.LinkUrl = GetElementValue(productElement, "url");
                product.StartDate = Convert.ToDateTime(GetElementValue(productElement, "startdate"));
                product.EndDate = Convert.ToDateTime(GetElementValue(productElement, "enddate"));
                product.ExpirationDate = Convert.ToDateTime(GetElementValue(productElement, "expirationDate"));
                product.DiscountPrice = DecimalManager.Parse(GetElementValue(productElement, "discount_price"));
                product.DiscountPercent = ByteManager.Parse(GetElementValue(productElement, "discountPercent"));

                products.Add(product);
            }
            catch (Exception ex)
            {
                ProductErrorLogItem productLogItem = new ProductErrorLogItem();
                productLogItem.Exception = ex;
                productLogItem.SiteProductID = product.SiteProductID;
                productLogItem.ProductXML = productElement.ToString();
                errorLogs.Add(productLogItem);
            }
        }

        ProductParseItem parseItem = new ProductParseItem();
        parseItem.Products = products;
        parseItem.LogItems = errorLogs;
        transaction.ReturnObject = parseItem;
        return transaction;
    }

    private static string GetElementValue(XElement element, string subElementName)
    {
        XElement subElement = element.Elements().FirstOrDefault(t => t.Name.LocalName == subElementName);
        if (subElement == null)
        {
            return string.Empty;
        }

        return subElement.Value;
    }

}
